<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace HelloAdmin\Controller;

/**
 * Description of BookController
 *
 * @author gangguo
 */
class BookController extends CommonController {
    //put your code here

    private $Book;
    private $BookArticle;


    public function __construct() {
        parent::__construct();
        $this->Book = new \Common\Model\BookModel();
        $this->BookArticle = new \Common\Model\BookArticleModel();
    }


    public function books(){

        $this->display('booklist');
    }

    public function articles(){
        $this->display('updatelist');
    }

    public function addBook(){
        $Cate = new \Common\Model\BookCateModel();
        $this->assign('cates',$Cate->select());
        $this->display();
    }

    public function addArticle(){
        //\Common\Library\COM\BookDo::getMyObject();
        $book_id = I('post.book_id');
        $article_name = I('post.article_name');
        if($article_name && $book_id > 0){
            $content = urldecode(I('post.content'));
            $ctime = I('post.create_time');
            $piece_name = I('post.piece_name');
            $pathFile = \Common\Library\COM\CollectDo::doContentPath();
            $data = array(
                    'book_id'=>$book_id,
                    'year'=>date('Y',time()),
                    'month'=>date('m',time()),
                    'day'=>date('d',time()),
                    'update_time'=>time(),
                    'unique'=>md5($book_id.$article_name),
                    'article_name'=>$article_name,
                    'article_num'=>\Common\Library\COM\CollectDo::getArticleNum($article_name),
                    'piece_name'=>$piece_name,
                    'piece_num'=>\Common\Library\COM\CollectDo::getPieceNum($piece_name),
                    'content'=>$pathFile,//urldecode($content),
                    'create_time'=>strtotime($ctime),
                );
            if(D('Common/BookArticle')->add($data)){
                file_put_contents('.'.$pathFile, htmlentities($content));
                $this->layerIfreamRe('添加成功');
            }
            $this->layerIfreamRe('添加失败','','error');
           exit;
        }

        $book_id = I('request.book_id');
        if($book_id > 0){
            $this->assign('book_name',  $this->Book->getBookName($book_id));
            $this->assign('book_id',$book_id);
        }


        $this->display();
    }


    public function articleList(){
        $search = I('post.search');
        $searchkey  = I('post.searchkey');
        if(empty($search)){
           $where = '1=1';
        }elseif($searchkey == 'article_name'){
            $where = "`article_name` like '{$search}%'";
        }elseif($searchkey == 'book_name'){
            $book = $this->Book->where("`book_name` like '{$search}'")->field('book_id')->find();
            $book_id = $book['book_id'];
            if($book_id > 0){
                $where = "`book_id` = '{$book_id}'";
            }else{
                $where = "`book_id` = '0'";
            }

        }
        $this->BookArticle->_useCache = false;
        $data = $this->BookArticle->getList($where, '', $this->offset, $this->limit);
        echo (json_encode($data));
    }

    public function bookList(){
       $search = I('post.search');
        if(empty($search)){
           $where = '1=1';
        }else{
            $where = "`book_name` like '{$search}%'";
        }
        $this->Book->_useCache = false;
        $data = $this->Book->getList($where,'',$this->offset,$this->limit);
        echo (json_encode($data));
    }

    public function getName(){
        $book_id = I('post.book_id');
        if($book_id > 0  && IS_AJAX){
            return $this->Book->getBookName();
        }
    }

    public function content(){
        $article_id = I('get.article_id');
        if($article_id > 0){
            $articles = $this->BookArticle->where("`article_id`='{$article_id}'")->find();
            $content = file_get_contents('.'.$articles['content']);
            //p(file_exists('.'.$articles['content']));
            //p(html_entity_decode(html_entity_decode($data['content'])));
            //echo ($data['content']);exit;
            $this->assign('data',$articles);
            $this->assign('article_id',$article_id);
            $this->assign('content',$content);
            $this->display('content');
        }
        return FALSE;

    }

    //删除章节
    public function delArticle(){
        $ids = I('post.ids');
        if(empty($ids)){
            return FALSE;
        }
        if($this->BookArticle->delArtilce($ids)){
            echo 'success';exit;
        }
        echo 'error';
    }


    //检查书本名称是否正确
    public function checkBookName(){
        $book_id = I('post.book_id');
        $book_name = I('post.book_name');
        if(empty($book_name)){
             exit('false');
        }
        if(\Common\Library\COM\BookDo::checkBookName($book_name)){
            exit('true');
        }
        exit('false');
    }


    //检查章节名称是否重复
    public function checkArtcileRepeat(){
        $article_name = I('post.article_name');
        $book_id = I('post.book_id');
        if(empty($article_name) || empty($book_id)){
            exit('false');
        }
        if($this->BookArticle->where("`book_id`='{$book_id}' AND `article_name`='{$article_name}'")->find()){
                exit('false');
        }
        exit('true');
    }

    //跟新保存章节内容
    public function updateContent(){
        $pathFile = '.'.I('post.pathFile');
        $content = I('post.content');
        if(file_exists($pathFile)){
            $handle = fopen($pathFile, 'w+');
            if(fwrite($handle, urldecode($content))){
                fclose($handle);
                exit('success');
            }
        }
    }

    public function delBooks(){
        $book_id = I('post.book_id');
        if($book_id > 0){
            if($this->Book->delete($book_id)){
                $this->BookArticle->where("`book_id`='{$book_id}'")->delete();
                exit('success');
            }

        }
        exit('error');
    }

    public function editBook(){
        $book_id = I('get.book_id');
        if($book_id > 0 && empty($_POST['book_id'])){
            $this->assign('cates',D('Common/BookCate')->select());
            $info = $this->Book->where("`book_id`='{$book_id}'")->find();
            $author = D('Common/BookAuthor')->where("`author_id`='{$info['author_id']}'")->find();
            $this->assign('author_name',$author['name']);
            $this->assign('info',$info);
            $this->display();exit;
        }
        $book_id = I('post.book_id');
        if($book_id > 0){
            $data['img'] = I('post.img');
            $data['cate_id'] = I('post.cate_id');
            $data['disabled'] = I('post.disabled');
            $data['tj_num'] = I('post.tj_num');
            $data['status'] = I('post.status');
            $data['introduction'] = urldecode(I('post.introduction'));

            if($this->Book->where("`book_id`='{$book_id}'")->save($data)){
                $this->layerIfreamRe('修改成功','Book/books');
            }
            $this->error('修改失败','Book/editBook/book_id/'.$book_id);

        }

    }
}
